package leetcode.code0481;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public int magicalString(int n) {
		if (n < 4) {
			return 1;
		}
		int ans = 0;
		int[] help = new int[n];
		help[0] = 1;
		int printIndex = 0;
		int printNum = 1;
		int printSize = 1;
		for (int i = 0; i < n; i++) {
			while (printIndex < n && printSize > 0) {
				help[printIndex++] = printNum;
				printSize--;
			}
			printNum = printNum == 1 ? 2 : 1;
			if (i + 1 < n) {
				if (help[i + 1] == 0) {
					printSize = printNum;
				} else {
					printSize = help[i + 1];
				}
			}
			ans += help[i] == 1 ? 1 : 0;
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(3, this.magicalString(6));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(1, this.magicalString(1));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
